package com.songtech.uitest.module.propose;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.KakongHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class GenerateTowerId {


    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(GenerateTowerId.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;


    public String generateTowerId(String step) throws Exception{

        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "接口模拟", "需求提出生成铁塔订单号", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        logger.info(space.getSpace() + "开始填写生成铁塔订单号页面");

        //填表单
        driver.findElement(By.id("mobileOrderCode")).sendKeys(indent.getMobileId());

        //需求来源细分种类，即建设类型
        Select demandResourceType = new Select(driver.findElement(By.id("demandResourceType")));
        if(indent.getCurrentBuildType().contains("新址")){
            demandResourceType.selectByIndex(0);
        }
        else{
            demandResourceType.selectByIndex(1);
        }


        driver.findElement(By.id("genernateCode")).click();
        Thread.sleep(500);

        //String towerId = driver.findElement(By.id("towerOrderCode")).getText();

        driver.findElement(By.id("saveSet")).click();

        Thread.sleep(3000);

        alertHandler.checkAndGo(driver);


        tabHandler.closeAllTabs(driver);
        //回到需求提出页获取铁塔侧订单号
        menuHandler.clickMenuItem(driver, "铁塔建设", "需求提出", "");

        //到页面上抓取铁塔侧订单号
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        //查询此单
        //待办状态设为所有
        Select taskStatus = new Select(driver.findElement(By.id("taskStatus")));
        taskStatus.selectByIndex(0);
        //输入单号查询
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(5000);

        //抓
        String towerId = driver.findElement(By.xpath("//*[@id='tb']/tbody/tr/td[7]")).getText();


        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        indent.setTowerId(towerId);

        logger.info(space.getSpace() + "铁塔侧订单号已生成，单号是" + towerId);

        tabHandler.closeAllTabs(driver);

        return towerId;

    }



}

